package com.citywithincity.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class StringUtils {
	
	
	public static final char BLANK = ' ';
	public static byte[] blank(int len){
		byte[] bytes = new byte[len];
		for(int i=0; i < len; ++i){
			bytes[i] = BLANK;
		}
		return bytes;
	}
	
	public static String blankStr(int len){
		StringBuilder sb = new StringBuilder();
		blankStr(sb,len);
		return sb.toString();
	}
	public static void blankStr(StringBuilder sb,int len){
		for(int i=0; i < len; ++i){
			sb.append( BLANK ) ;
		}
	}
	
	//替换驼峰式为下滑现
	public static String getHump2Underline(String src){
		
		char[] chars = src.toCharArray();
		StringBuilder sBuilder = new StringBuilder();
		for (char c : chars) {
			if(Character.isUpperCase(c)){
				sBuilder.append("_");
				sBuilder.append(Character.toLowerCase(c));
			}else{
				sBuilder.append(c);
			}
		}
		
		String result = sBuilder.toString();
		if(result.startsWith("_")){
			result = result.substring(1);
		}
		
		return result;
		
		
	}
	
	

	/**
	 * 获取有中文时候的实际长度
	 * 
	 * @param str
	 * @return
	 */
	public static int getRealLength(String str) {
		int length = 0;
		for (int i = 0, count = str.length(); i < count; i++) {
			int ascii = Character.codePointAt(str, i);
			if (ascii >= 0 && ascii <= 255)
				length++;
			else
				length += 2;

		}
		return length;
	}

	public static boolean isEmpty(String str) {
		if (str == null)
			return true;
		return str.trim().equals("");
	}

	/**
	 * setter
	 */
	public static String getSetterName(String name) {
		char firstChar = name.charAt(0);
		if (firstChar >= 'a' && firstChar <= 'z') {
			char[] arr = name.toCharArray();
			arr[0] += ('A' - 'a');
			return "set" + new String(arr);
		}
		return "set" + name;
	}

	/**
	 * setter
	 */
	public static String getGetterName(String name,boolean isboolean) {
		char[] chars = name.toCharArray();
		char firstChar = chars[0];
		if (firstChar >= 'a' && firstChar <= 'z') {
			firstChar += ('A' - 'a');
			chars[0] = firstChar;
		}
		if(isboolean){
			return new StringBuilder("is").append( chars).toString();
		}else{
			return new StringBuilder("get").append( chars).toString();
		}
	}

	/**
	 * getter
	 */

	/**
	 * 首字母变小写
	 */
	public static String firstCharToLowerCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'A' && firstChar <= 'Z') {
			char[] arr = str.toCharArray();
			arr[0] += ('a' - 'A');
			return new String(arr);
		}
		return str;
	}
	
	public static String join(String splitter,Object...values){
		StringBuilder sb = new StringBuilder();
		boolean first = true;
		for (Object string : values) {
			if(first){
				first = false;
			}else{
				sb.append(splitter);
			}
			sb.append(string);
		}
		return sb.toString();
	}
	public static String join(Collection<?> titles) {
		StringBuilder sb = new StringBuilder();
		boolean first = true;
		for (Object string : titles) {
			if(first){
				first = false;
			}else{
				sb.append(',');
			}
			sb.append(string);
		}
		return sb.toString();
	}
	public static String join(Object[] titles,String splitter) {
		StringBuilder sb = new StringBuilder();
		boolean first = true;
		for (Object string : titles) {
			if(first){
				first = false;
			}else{
				sb.append(splitter);
			}
			sb.append(string);
		}
		return sb.toString();
	}

	public static String join(Object[] titles) {
		return join(titles,",");
	}

	public static int length(String string) {
		
		return string==null ? 0 : string.length();
	}

	public static List<String> split(String src, String splitter) {
		if(src==null)return null;
		String[] args = src.split(splitter);
		List<String> list = new ArrayList<String>();
		for (String string : args) {
			list.add(string);
		}
		return list;
	}



	public static String leftPadZero(Object value, int len) {
		String src = String.valueOf(value);
		StringBuilder sb = new StringBuilder(len);
		for(int i=src.length(); i<len; ++i){
			sb.append("0");
		}
		sb.append(src);
		return sb.toString();
	}



	public static String ltrim(String src,char sp) {
		if(src==null){
			return src;
		}
		for(int i=0 , c=src.length(); i < c; ++i){
			if(src.charAt(i) != sp){
				return src.substring(i);
			}
		}
		return src;
	}

	public static String rtrim(String src, char sp) {
		for(int i=src.length()-1; i>=0; --i){
			if(src.charAt(i) != sp){
				return src.substring(0,i+1);
			}
		}
		return src;
	}
}
